<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="规则名称">
              <a-input placeholder="请输入规则名称" v-model="queryParam.name" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="门类型">
              <j-dict-select-tag
                v-model="queryParam.type"
                placeholder="请选择门类型"
                dictCode="window_door_type"
                allowClear
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="启用状态">
              <j-dict-select-tag
                v-model="queryParam.enabled"
                placeholder="请选择启用状态"
                dictCode="enable_status"
                allowClear
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('询单规则')">导出</a-button>
      <a-button @click="batchDel" v-if="selectedRowKeys.length > 0" ghost type="primary" icon="delete"
        >批量删除</a-button
      >
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchHandleEnabled(1)"> <a-icon type="check" />批量启用 </a-menu-item>
          <a-menu-item key="0" @click="batchHandleEnabled(0)"> <a-icon type="stop" />批量禁用 </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /> </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="rowSelection"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <template slot="type" slot-scope="text">
          <a-tag v-if="text === 0" color="blue">推拉</a-tag>
          <a-tag v-else-if="text === 1" color="green">平开</a-tag>
          <span v-else>{{ text }}</span>
        </template>

        <template slot="isMust" slot-scope="text">
          <a-tag v-if="text === 0" color="orange">否</a-tag>
          <a-tag v-else-if="text === 1" color="red">是</a-tag>
          <span v-else>{{ text }}</span>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定要切换启用状态吗?" @confirm="() => handleToggleEnabled(record)">
            <a>{{ record.enabled === 1 ? '禁用' : '启用' }}</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleCopy(record)">复制</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <inquiry-rule-modal ref="modalForm" @ok="modalFormOk"></inquiry-rule-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import InquiryRuleModal from './modules/InquiryRuleModal'

export default {
  name: 'InquiryRuleList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    InquiryRuleModal,
  },
  data() {
    return {
      description: '询单规则管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '规则名称',
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: '门类型',
          align: 'center',
          dataIndex: 'type',
          scopedSlots: { customRender: 'type' },
        },
        {
          title: '是否必填',
          align: 'center',
          dataIndex: 'isMust',
          scopedSlots: { customRender: 'isMust' },
        },
        {
          title: '排序号',
          align: 'center',
          dataIndex: 'sortNo',
        },
        {
          title: '启用状态',
          align: 'center',
          dataIndex: 'enabled',
          customRender: (text) => {
            if (text === 1) {
              return <a-tag color="green">启用</a-tag>
            } else {
              return <a-tag color="red">禁用</a-tag>
            }
          },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
          sorter: true,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/oms/inquiry/rule/list',
        delete: '/oms/inquiry/rule/delete',
        deleteBatch: '/oms/inquiry/rule/deleteBatch',
        exportXlsUrl: '/oms/inquiry/rule/exportXls',
        importExcelUrl: 'oms/inquiry/rule/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
        onSelectAll: this.onSelectAll,
      }
    },
  },
  methods: {
    initDictConfig() {
      // 初始化字典配置
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'name', text: '规则名称' })
      fieldList.push({ type: 'int', value: 'type', text: '门类型' })
      fieldList.push({ type: 'int', value: 'isMust', text: '是否必填' })
      fieldList.push({ type: 'int', value: 'sortNo', text: '排序号' })
      fieldList.push({ type: 'int', value: 'enabled', text: '启用状态' })
      fieldList.push({ type: 'string', value: 'remark', text: '备注' })
      this.superFieldList = fieldList
    },
    handleAdd: function () {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增询单规则'
    },
    handleEdit: function (record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑询单规则'
    },
    handleDetail: function (record) {
      this.$refs.modalForm.detail(record)
      this.$refs.modalForm.title = '询单规则详情'
    },
    handleCopy: function (record) {
      this.$refs.modalForm.copy(record)
      this.$refs.modalForm.title = '复制询单规则'
    },
    handleToggleEnabled: function (record) {
      let that = this
      let newEnabled = record.enabled === 1 ? 0 : 1
      this.$http
        .post('/oms/inquiry/rule/updateEnabled', {
          id: record.id,
          enabled: newEnabled,
        })
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.loadData()
          } else {
            that.$message.warning(res.message)
          }
        })
    },
    batchHandleEnabled: function (enabled) {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return false
      } else {
        let that = this
        this.$confirm({
          title: '确认操作',
          content: `确定要${enabled === 1 ? '启用' : '禁用'}选中的规则吗?`,
          onOk: function () {
            let promises = that.selectedRowKeys.map((id) => {
              return that.$http.post('/oms/inquiry/rule/updateEnabled', {
                id: id,
                enabled: enabled,
              })
            })

            Promise.all(promises)
              .then(() => {
                that.$message.success('操作成功!')
                that.loadData()
                that.onClearSelected()
              })
              .catch(() => {
                that.$message.error('操作失败!')
              })
          },
        })
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
